import 'package:demo202112/utils/common_appbar.dart';
import 'package:flutter/material.dart';

/// @Author wywinstonwy
/// @Date 2022/2/4 11:10 上午
/// @Description:
///


class MyNotificationPage extends StatefulWidget {
  const MyNotificationPage({Key? key}) : super(key: key);

  @override
  _MyNotificationPageState createState() => _MyNotificationPageState();
}

class _MyNotificationPageState extends State<MyNotificationPage> {
  String _msg="";
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar('Notification'),
      body:_buildView2() ,
    );
  }

  _buildView(){
    return NotificationListener(
       onNotification:(Notification notification){
         switch(notification.runtimeType){
           case ScrollStartNotification :print('开始滚动');break;
           case ScrollUpdateNotification: print("正在滚动"); break;
           case ScrollEndNotification: print("滚动停止"); break;
           case OverscrollNotification: print("滚动到边界"); break;
           case UserScrollNotification:print('UserScrollNotification');break;

         }
         return true;
       } ,
        child: ListView.builder(
          itemCount: 100,
            itemBuilder: (context,index){
          return ListTile(title: Text("$index"),);
        }));
  }
  
  _buildView2(){
    return NotificationListener<Mynotification>(
       onNotification: (notification){
         setState(() {
           _msg += notification.msg+" ";
         });
         return true;
       },
      
        child: Center(child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
          Builder(builder: (context){
            return ElevatedButton(onPressed: () { 
              Mynotification('Hi').dispatch(context);
            },
            child: Text('Send Notification'),);
          }),
            Text(_msg)
        ],),));
  }
}

class Mynotification extends Notification{
  final String msg;
  Mynotification(this.msg);
}

